/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.common.defaults;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.balisunrise.application.Application;
import org.balisunrise.common.Query;
import org.balisunrise.common.EntityList;
import org.balisunrise.common.EntityLoader;
import org.balisunrise.common.VisualEntity;
import org.balisunrise.common.abstracts.AbstractBasic;
import org.balisunrise.common.listeners.EntityLoadListener;

/**
 * Um EntityLoader que não faz acesso a dados, não utiliza um DAO.
 * Os dados exibidos por este EntityLoader são fixos.
 * 
 * @author Glauco
 */
public class SimpleEntityLoader<C extends Serializable> extends AbstractBasic
        implements EntityLoader{

    private VisualEntity visualEntity;
    private Class entityClass;
    private EntityList entityList;
    private List<EntityLoadListener> listeners;

    public SimpleEntityLoader(){
        listeners = new ArrayList<EntityLoadListener>();
    }

    public SimpleEntityLoader(List<C> list) {

        if(list != null && list.size() > 0)
            this.entityClass = list.get(0).getClass();

        entityList = Application.get().getFactory().createEntityList();
        entityList.setOwner(this);

        if(list != null)
            entityList.addList(list);
    }

    public void setEntityClass(Class entityClass){
        this.entityClass = entityClass;
    }

    public void setVisualEntity(VisualEntity visualEntity) {
        this.visualEntity = visualEntity;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Class getEntityClass() {
        return entityClass;
    }

    @Override
    public void setEntityList(EntityList entityList) {
        this.entityList = entityList;
    }

    @Override
    public EntityList getEntityList() {
        return entityList;
    }

    @Override
    public void setMaxResults(int maxResults) {

    }

    @Override
    public int getMaxResults() {
        return 0;
    }

    @Override
    public void load(Query query) {

    }

    @Override
    public void iterate() {

    }

    @Override
    public VisualEntity getOwner() {
        return visualEntity;
    }

    @Override
    public void addEntityLoadListener(EntityLoadListener listener) {
        listeners.add(listener);
    }

    @Override
    public boolean removeEnityLoadListener(EntityLoadListener listener) {
        return listeners.remove(listener);
    }

    @Override
    public EntityLoadListener[] getEntityLoadListeners() {
        return listeners.toArray(new EntityLoadListener[]{});
    }

}
